package com.emex.app.stuff.provider;

import com.emex.app.shared.data.Cost;
import com.emex.app.shared.data.CostType;
import com.emex.app.shared.data.PlanFact;
import com.emex.app.shared.ws.CostWebService;
import com.emex.app.stuff.utils.WebServiceHelper;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.List;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;

/**
 *
 * @author Sergey Karavaev
 */
public class CostDataProvider extends Service {

    private CostWebService service = null;

    private static CostDataProvider instance = null;

    /**
     * Constructor
     */
    private CostDataProvider() throws MalformedURLException {
        super(new URL(WebServiceHelper.getDefault().getWebServiceURL("web", "Cost")),
                WebServiceHelper.getDefault().getWebServiceQName("Cost"));
        service = getPort(CostWebService.class);
    }

    /**
     * Get instance of data provider.
     *
     * @return data provider instance
     */
    public static synchronized CostDataProvider getInstance() {
        if (instance == null) {
            new RequestWebService() {
                @Override
                protected void doRequestBody() throws MalformedURLException {
                    instance = new CostDataProvider();
                }
            }.doRequest();
        }
        return instance;
    }

    public BindingProvider getBindingProvider() {
        return (BindingProvider) service;
    }

    public int deleteCostByPrimaryKey(Integer id) {
        return service.deleteCostByPrimaryKey(id);
    }

    public Cost insertCost(Cost record) {
        return service.insertCost(record);
    }

    public Cost selectCostByPrimaryKey(Integer id) {
        return service.selectCostByPrimaryKey(id);
    }

    public Cost updateCostByPrimaryKey(Cost record) {
        record = service.updateCostByPrimaryKey(record);
        return record;
    }

    public List<Cost> getAllCostsByDate(Date date) {
        return service.getAllCostsByDate(date);
    }

    public int deleteCostTypeByPrimaryKey(Integer id) {
        return service.deleteCostTypeByPrimaryKey(id);
    }

    public CostType insertCostType(CostType record) {
        return service.insertCostType(record);
    }

    public List<CostType> getAllCostTypes() {
        return service.getAllCostTypes();
    }

    public CostType updateCostType(CostType record) {
        return service.updateCostType(record);
    }

    public CostType selectCostTypeByPrimaryKey(Integer id) {
        return service.selectCostTypeByPrimaryKey(id);
    }

    public List<Cost> getAllCostsByDateAndCostType(Date startDate, Date endDate, Integer costTypeId) {
        return service.getAllCostsByDateAndCostType(startDate, endDate, costTypeId);
    }

    public List<Cost> getAllCostsByDateEqualOrGrater(Date date) {
        return service.getAllCostsByDateEqualOrGrater(date);
    }

    public PlanFact insertPlanFact(PlanFact record) {
        return service.insertPlanFact(record);
    }

    public PlanFact updatePlanFact(PlanFact record) {
        return service.updatePlanFact(record);
    }

    public PlanFact getCurrentMonthPlanFact() {
        return service.getCurrentMonthPlanFact();
    }
}
